package com.huiminpay.message;

import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @ClassName ProducerSimple
 * @Author: longlong
 * @CreateDate: 2023/12/18 11:30
 */

@Component
public class ProducerSimple {
    @Autowired
    RocketMQTemplate rocketMQTemplate;


    /**
     * 发送同步消息
     */
    public void sendSyncMsg(String topic, String msg) {
        rocketMQTemplate.syncSend(topic, msg);
        System.out.println("消息发送成功！");
    }

    /**
     * 发送异步消息
     *
     * @param:
     * @return:
     */
    public void sendASyncMsg(String topic, String msg) {
        rocketMQTemplate.asyncSend(topic, msg, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                //成功回调
                System.out.println(sendResult.getSendStatus());
            }

            @Override
            public void onException(Throwable throwable) {
                //异常回调
                System.err.println(throwable.getMessage());
            }
        });

    }


    /**
     \* 发送单向消息
     \* @param topic
     \* @param msg
     */
    public void sendOneWayMsg(String topic, String msg){
        this.rocketMQTemplate.sendOneWay(topic,msg);
    }


}
